/*-------------------------------------------------------------------------------------------------------- */
if (true) {
    var foo = 'sss';
}

console.log(foo)
//  var在块级外也能拿到变量
/*-------------------------------------------------------------------------------------------------------- */

//  数组的解构
const arr = [1, 2, 3];
// const [a, b, c] = arr;
// const [a, ...rest] = arr;
const [a, b, c, d = 4] = arr;
console.log(a, b, c, d)
/*-------------------------------------------------------------------------------------------------------- */

//  模板字符串
const name = 'tom';
const age = 18;
const fn = string => console.log(string)

const result = fn`my name is ${name}, i am ${age} yaers old`;
console.log(result)
/*-------------------------------------------------------------------------------------------------------- */

//  字符串的扩展方法
const message = 'Error: foo is not defined.'
console.log(message.includes('foo'));
console.log(message.startsWith('Error'))
console.log(message.startsWith('e'))
console.log(message.endsWith('e'))
console.log(message.endsWith('.'))

/*-------------------------------------------------------------------------------------------------------- */

//  计算属性名
const aaa = 'xxx'
const obj = {
    [aaa] : 123
}
console.log(obj)
/*-------------------------------------------------------------------------------------------------------- */

const person1 = {
    name: 'tom',
    sayHi: () => {
        console.log(`Hi, my name is ${this.name}`)
    }
}
person1.sayHi()

/*-------------------------------------------------------------------------------------------------------- */
const obj1 = {
    aaa: 1,
    bbb: 2,
}

const obj2 = {
    aaa: 3,
    bbb: 2,
    ccc: 4
}

const obj3 = Object.assign(obj1, obj2)
console.log(obj3)
console.log(obj1 === obj3)

/*-------------------------------------------------------------------------------------------------------- */

//  Object.is

console.log(Object.is('foo', 'foo'))
console.log(Object.is('+0', '-0'))
console.log(Object.is(NaN, NaN))
console.log(NaN === NaN)

/*-------------------------------------------------------------------------------------------------------- */

const person = {
    name: 'tom',
    age: 18
}

const personProxy = new Proxy(person, {
    get(target, property) {
        return target[property] ? target[property] : 'default';
    },
    set(target, property, value) {
        console.log(target, property, value)
        target[property] = value;
    },
    deleteProperty(target, property) {
        console.log(property);
        delete target[property]
    }
})
personProxy.height = 172;
delete personProxy.height;

const arr1 = [];
const arrProxy = new Proxy(arr1, {
    set(target, property, value) {
        console.log(target, property);
        target[property] = value;
        return true;
    }
})
arrProxy.push(100);
/*-------------------------------------------------------------------------------------------------------- */

//  reflect

const aaabbb = {
    name: 'tome',
    age: 18
}

console.log(Reflect.has(aaabbb, 'name'))
console.log(Reflect.deleteProperty(aaabbb, 'age'));
console.log(Reflect.ownKeys(aaabbb))

/*-------------------------------------------------------------------------------------------------------- */

const setArr = new Set();
setArr.add(1).add(2)
console.log(setArr)

/*-------------------------------------------------------------------------------------------------------- */

const mapObj = new Map();
mapObj.set({aaa: 1}, {bbb: 2});
console.log(mapObj)

/*-------------------------------------------------------------------------------------------------------- */

console.log(Symbol('foo') === Symbol('foo'))
const s1 = Symbol.for('foo');
const s2 = Symbol.for('foo');
console.log(s1 === s2)
console.log(Symbol.for(true) === Symbol.for('true'))
const symbolObj = {
    [Symbol()]: 'aaa',
    aaa: 123
}
console.log(Reflect.ownKeys(symbolObj))
//  [ 'aaa', Symbol() ]
console.log(Object.keys(symbolObj))
//  [ 'aaa' ]
console.log(Object.getOwnPropertySymbols(symbolObj))
//  [ Symbol() ]

/*-------------------------------------------------------------------------------------------------------- */

const arrForof = [100, 200, 300, 400];
for (const item of arrForof) {
    console.log(item)
}

const setForof = new Set(['a', 'b', 'c'])
for (const item of setForof) {
    console.log(item)
}

const mapForof = new Map();
mapForof.set('a', 1)
mapForof.set('b', 2)
mapForof.set('c', 3)
for (const item of mapForof) {
    console.log(item)
}

/*-------------------------------------------------------------------------------------------------------- */
